<!DOCTYPE html>
<html lang="en" style="height: 100%;">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <script src="https://unpkg.com/vue@3.0.5/dist/vue.global.js"></script> -->
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css" />
    <script src="https://unpkg.com/element-plus"></script>
    <script src="//unpkg.com/element-plus/dist/locale/zh-cn"></script>
    <title>只有功能展示，没有美化</title>
</head>
<body style="height: 100%;">
    <div id="layoutPage">
        <div class="top">
            <div class="Logo">
                <span>BlogSystem</span>
            </div>
            <div class="top-menu">
                <ul>
                    <li>首頁</li>
                    <li>收藏本站</li>
                </ul>
            </div>
            <div class="Login-pad">
                <div v-if="userInfo == null">
                <label>
                    <span>用戶名: </span>
                    <input type="userName" v-model="userName" id="">
                </label>
                <label>
                    <span>密碼: </span>
                    <input type="password" v-model="password" id="">
                </label>
                <button @click="onLogin">登录</button>
                </div>
                <div v-else>
                    <span>用户：{{userInfo.username}}</span>
                    
                    <button @click="onLoginout">注销</button>
                </div>
            </div>
        </div>
        <div class="center">
           <iframe src="./index.html" frameborder="0" name="child"></iframe>
        </div>
        <div class="bottom"></div>
    </div>
</body>

<script>
    const {createApp, ref, onMounted } = Vue;
    function toRouter() {
        var hash = location.hash       
        if (hash == "")
            return "./index.html";
        else
            return hash.replace("#",".")
    }

    const userInfo = ref()
    function clearUserInfo()
    {
        localStorage.clear();
        userInfo.value = null;
    }
    const app = createApp({
            setup() {

                const userName = ref("")
                const password = ref("")
                const onLogin = () => {
                    let name = userName.value;
                    let pwd = password.value;
                    axios.get(`http://localhost:5143/api/Login/CheckLogin?userName=${name}&password=${pwd}`)
                    .then(res=>{
                        if(res.status == 200){
                            userName.value = "";
                            password.value = "";
                            userInfo.value = res.data
                            localStorage["token"] = res.data.token;
                            localStorage["userInfo"] = JSON.stringify(res.data);
                            child.window.getToken()
                        }
                        //console.log(res);
                    });
                }
                const onLoginout = () =>{
                    userInfo.value = null;
                    localStorage.clear();
                    child.window.getToken();
                }
                const tranKey =(key) =>
                {
                   return key.replaceAll(",","，");
                } 
                const tranTime =(time) => {
                    return time.replaceAll("T"," ");
                }
                onMounted(() =>{
                    window.document.querySelector("iframe").src = toRouter();
                    if (localStorage["userInfo"] != null) {
                        var strUserInfo = localStorage["userInfo"];
                        userInfo.value = JSON.parse(strUserInfo);
                    }
                })
                return{
                    userName,
                    password,
                    onLogin,
                    onLoginout,
                    userInfo,
                    tranKey,
                    tranTime,
                };
            }
        }
    )
    app.use(ElementPlus, {
    locale: ElementPlusLocaleZhCn,
    })
    app.mount("#layoutPage")
</script>

<style>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    #layoutPage{
        height: 100%;
    }
    #layoutPage .top{
        background-color: cornflowerblue;
        height: 200px;
        position: relative;
    }

    #layoutPage .top .top-menu{
        float:right
    }
    #layoutPage .top .top-menu li{
        list-style: none;
        float:left;
        margin-right: 10px;
    }
    #layoutPage .top .Login-pad{
        position: absolute;
        bottom: 0;
        right: 0;
        height: 46px;
        padding: 10px;
        padding-bottom: 6px;
        border-radius: 6px 0 0 0;
        background-color: beige;
    }
    #layoutPage .center{
        display: flex;
        height: 100%;
    }
    #layoutPage .center iframe{
        height: 100%;
        width: 100%;
    }
</style>
</html>